#!/bin/bash
#author:	rengyi yan
#date Time:	2015-08-03 10:40:41
#descriptions:	auto install nginx by source code

#configfile: /etc/nginx/conf/nginx.conf
#startup :   service nginx start


#step 1:    install dependencies for nginx module support
    yum -y install gcc gcc-c++ autoconf automake
    
    cd ../softwares/deps/
    tar xzvf zlib-1.2.8.tar.gz
    cd zlib-1.2.8
    ./configure --prefix=/usr/local/zlib
    make && make install
    cd ../
    mv zlib-1.2.8/ /usr/local/zlib-1.2.8

    tar xzvf pcre-8.36.tar.gz
    cd pcre-8.36
    ./configure --prefix=/usr/local/pcre
    make && make install
    cd ../
    mv pcre-8.36/ /usr/local/pcre-8.36

    tar xzvf openssl-1.0.2d.tar.gz
    cd openssl-1.0.2d
    ./config
    make && make install
    cd ../
    mv openssl-1.0.2d/ /usr/local/openssl-1.0.2d


#step 2:    kernel arguments setting
    sed -i '$afs.file-max = 999999' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_tw_reuse = 1' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_keepalive_time = 600' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_fin_timeout = 30' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_max_tw_buckets = 5000' /etc/sysctl.conf
    sed -i '$anet.ipv4.ip_local_port_range= 1024 61000' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_rmem = 4096 32768 262142' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_wmem = 4096 32768 262142' /etc/sysctl.conf
    sed -i '$anet.core.netdev_max_backlog = 8096' /etc/sysctl.conf
    sed -i '$anet.core.rmem_default = 262144' /etc/sysctl.conf
    sed -i '$anet.core.wmem_default = 262144' /etc/sysctl.conf
    sed -i '$anet.core.rmem_max = 2097152' /etc/sysctl.conf
    sed -i '$anet.core.wmem_max = 2097152' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_syncookies = 1' /etc/sysctl.conf
    sed -i '$anet.ipv4.tcp_max_syn_backlog = 1024' /etc/sysctl.conf

#step 3:    install nginx and setting the config file
    cd ../
    tar xzvf nginx-1.8.0.tar.gz
    cd nginx-1.8.0
    ./configure --prefix=/usr/local/nginx \
                --user=nobody  \
                --group=nobody  \
                --conf-path=/etc/nginx/conf/nginx.conf \
                --pid-path=/var/run/nginx.pid \
                --lock-path=/var/lock/subsys/nginx \
                --error-log-path=/var/log/nginx/error.log \
                --http-log-path=/var/log/nginx/access.log \
                --with-poll_module \
                --with-file-aio \
                --with-http_ssl_module \
                --with-pcre=/usr/local/pcre-8.36 \
                --with-zlib=/usr/local/zlib-1.2.8 \
                --with-openssl=/usr/local/openssl-1.0.2d \
                --with-http_ssl_module \
                --with-http_gzip_static_module \
                --with-http_stub_status_module \
                --with-http_gunzip_module \
                --with-http_dav_module \
                --with-http_realip_module
    
    make
    make install
    
    mkdir -p /etc/nginx/conf/
    mkdir -p /var/log/nginx/

    cp ../../conf.d/nginx.conf  /etc/nginx/conf/nginx.conf

#step 4:    validate the sysctl.conf
    sysctl -p

#finally:
    cd ../
    rm -rf nginx-1.8.0

if [ $? -eq 0 ]
then
    echo "install done!"
else
    echo "install failure!"
fi
